home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Tool Chest / Dev.CD Feb 97 TC.toast / Sample Code / Files / MoreFiles 1.4.4 / Sources / MoreDesktopMgr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-17  |  31.7 KB  |  1,242 lines  |  [TEXT/MPS ]

  1. /*
  2. **    Apple Macintosh Developer Technical Support
  3. **
  4. **    A collection of useful high-level Desktop Manager routines.
  5. **    If the Desktop Manager isn't available, use the Desktop file
  6. **    for 'read' operations.
  7. **
  8. **    We do more because we can...
  9. **
  10. **    by Jim Luther and Nitin Ganatra, Apple Developer Technical Support Emeriti
  11. **
  12. **    File:    MoreDesktopMgr.c
  13. **
  14. **    Copyright © 1992-1996 Apple Computer, Inc.
  15. **    All rights reserved.
  16. **
  17. **    You may incorporate this sample code into your applications without
  18. **    restriction, though the sample code has been provided "AS IS" and the
  19. **    responsibility for its operation is 100% yours.  However, what you are
  20. **    not permitted to do is to redistribute the source as "DSC Sample Code"
  21. **    after having made changes. If you're going to re-distribute the source,
  22. **    we require that you make it clear in the source that the code was
  23. **    descended from Apple Sample Code, but that you've made changes.
  24. */
  25.  
  26. #include <Types.h>
  27. #include <Errors.h>
  28. #include <Memory.h>
  29. #include <Files.h>
  30. #include <Resources.h>
  31. #include <Icons.h>
  32.  
  33. #define    __COMPILINGMOREFILES
  34.  
  35. #include "MoreFiles.h"
  36. #include "MoreFilesExtras.h"
  37. #include "Search.h"
  38. #include "MoreDesktopMgr.h"
  39.  
  40. /*****************************************************************************/
  41.  
  42. /*    Desktop file notes:
  43. **
  44. **    •    The Desktop file is owned by the Finder and is normally open by the
  45. **        Finder. That means that we only have read-only access to the Desktop
  46. **        file.
  47. **    •    Since the Resource Manager doesn't support shared access to resource
  48. **        files and we're using read-only access, we don't ever leave the
  49. **        Desktop file open.  We open a path to it, get the data we want out
  50. **        of it, and then close the open path. This is the only safe way to
  51. **        open a resource file with read-only access since some other program
  52. **        could have it open with write access.
  53. **    •    The bundle related resources in the Desktop file are normally
  54. **        purgable, so when we're looking through them, we don't bother to
  55. **        release resources we're done looking at - closing the resource file
  56. **        (which we always do) will release them.
  57. **    •    Since we can't assume the Desktop file is named "Desktop"
  58. **        (it probably is everywhere but France), we get the Desktop
  59. **        file's name by searching the volume's root directory for a file
  60. **        with fileType == 'FNDR' and creator == 'ERIK'. The only problem with
  61. **        this scheme is that someone could create another file with that type
  62. **        and creator in the root directory and we'd find the wrong file.
  63. **        The chances of this are very slim.
  64. */
  65.  
  66. /*****************************************************************************/
  67.  
  68. /* local defines */
  69.  
  70. enum
  71. {
  72.     kBNDLResType    = 'BNDL',
  73.     kFREFResType    = 'FREF',
  74.     kIconFamResType    = 'ICN#',
  75.     kFCMTResType    = 'FCMT',
  76.     kAPPLResType    = 'APPL'
  77. };
  78.  
  79. /*****************************************************************************/
  80.  
  81. /* local data structures */
  82.  
  83. #if PRAGMA_ALIGN_SUPPORTED
  84. #pragma options align=mac68k
  85. #endif
  86.  
  87. struct IDRec
  88. {
  89.     short        localID;
  90.     short        rsrcID;
  91. };
  92. typedef struct IDRec IDRec;
  93. typedef    IDRec *IDRecPtr;
  94.  
  95. struct BundleType
  96. {
  97.     OSType        type;            /* 'ICN#' or 'FREF' */
  98.     short        count;            /* number of IDRecs - 1 */
  99.     IDRec        idArray[1];
  100. };
  101. typedef struct BundleType BundleType;
  102. typedef BundleType *BundleTypePtr;
  103.  
  104. struct BNDLRec
  105. {
  106.     OSType        signature;        /* creator type signature */
  107.     short        versionID;        /* version - should always be 0 */
  108.     short        numTypes;        /* number of elements in typeArray - 1 */
  109.     BundleType    typeArray[1];
  110. };
  111. typedef struct BNDLRec BNDLRec;
  112. typedef BNDLRec **BNDLRecHandle;
  113.  
  114. struct FREFRec
  115. {
  116.     OSType        fileType;        /* file type */
  117.     short        iconID;            /* icon local ID */
  118.     Str255        fileName;        /* file name */
  119. };
  120. typedef struct FREFRec FREFRec;
  121. typedef FREFRec **FREFRecHandle;
  122.  
  123. struct APPLRec
  124. {
  125.     OSType        creator;        /* creator type signature */
  126.     long        parID;            /* parent directory ID */
  127.     Str255        applName;        /* application name */
  128. };
  129. typedef struct APPLRec APPLRec;
  130. typedef APPLRec *APPLRecPtr;
  131.  
  132. #if PRAGMA_ALIGN_SUPPORTED
  133. #pragma options align=reset
  134. #endif
  135.  
  136. /*****************************************************************************/
  137.  
  138. /* static prototypes */
  139.  
  140. static    OSErr    GetDesktopFileName(short vRefNum,
  141.                                    Str255 desktopName);
  142.  
  143. static    OSErr    GetAPPLFromDesktopFile(ConstStr255Param volName,
  144.                                        short vRefNum,
  145.                                        OSType creator,
  146.                                        short *applVRefNum,
  147.                                        long *applParID,
  148.                                        Str255 applName);
  149.  
  150. static    OSErr    FindBundleGivenCreator(OSType creator,
  151.                                        BNDLRecHandle *returnBndl);
  152.                                        
  153. static    OSErr    FindTypeInBundle(OSType typeToFind,
  154.                                  BNDLRecHandle theBndl,
  155.                                  BundleTypePtr *returnBundleType);
  156.                                          
  157. static    OSErr    GetLocalIDFromFREF(BundleTypePtr theBundleType,
  158.                                    OSType fileType,
  159.                                    short *iconLocalID);
  160.  
  161. static    OSErr    GetIconRsrcIDFromLocalID(BundleTypePtr theBundleType,
  162.                                          short iconLocalID,
  163.                                          short *iconRsrcID);
  164.  
  165. static    OSType    DTIconToResIcon(short iconType);
  166.  
  167. static    OSErr    GetIconFromDesktopFile(ConstStr255Param volName,
  168.                                        short vRefNum,
  169.                                        short iconType,
  170.                                        OSType fileCreator,
  171.                                        OSType fileType,
  172.                                        Handle *iconHandle);
  173.  
  174. static    OSErr    GetCommentID(short vRefNum,
  175.                              long dirID,
  176.                              ConstStr255Param name,
  177.                              short *commentID);
  178.  
  179. static    OSErr    GetCommentFromDesktopFile(short vRefNum,
  180.                                           long dirID,
  181.                                           ConstStr255Param name,
  182.                                           Str255 comment);
  183.  
  184. /*****************************************************************************/
  185.  
  186. /*
  187. **    GetDesktopFileName
  188. **
  189. **    Get the name of the Desktop file.
  190. */
  191. static    OSErr    GetDesktopFileName(short vRefNum,
  192.                                    Str255 desktopName)
  193. {
  194.     OSErr            error;
  195.     HParamBlockRec    pb;
  196.     short            index;
  197.     Boolean            found;
  198.     
  199.     pb.fileParam.ioNamePtr = desktopName;
  200.     pb.fileParam.ioVRefNum = vRefNum;
  201.     pb.fileParam.ioFVersNum = 0;
  202.     index = 1;
  203.     found = false;
  204.     do
  205.     {
  206.         pb.fileParam.ioDirID = fsRtDirID;
  207.         pb.fileParam.ioFDirIndex = index;
  208.         error = PBHGetFInfoSync(&pb);
  209.         if ( error == noErr )
  210.         {
  211.             if ( (pb.fileParam.ioFlFndrInfo.fdType == 'FNDR') &&
  212.                  (pb.fileParam.ioFlFndrInfo.fdCreator == 'ERIK') )
  213.             {
  214.                 found = true;
  215.             }
  216.         }
  217.         ++index;
  218.     } while ( (error == noErr) && !found );
  219.     
  220.     return ( error );
  221. }
  222.  
  223. /*****************************************************************************/
  224.  
  225. pascal    OSErr    DTOpen(ConstStr255Param volName,
  226.                        short vRefNum,
  227.                        short *dtRefNum,
  228.                        Boolean *newDTDatabase)
  229. {
  230.     OSErr error;
  231.     GetVolParmsInfoBuffer volParmsInfo;
  232.     long infoSize;
  233.     DTPBRec pb;
  234.     
  235.     /* Check for volume Desktop Manager support before calling */
  236.     infoSize = sizeof(GetVolParmsInfoBuffer);
  237.     error = HGetVolParms(volName, vRefNum, &volParmsInfo, &infoSize);
  238.     if ( error == noErr )
  239.     {
  240.         if ( hasDesktopMgr(volParmsInfo) )
  241.         {
  242.             pb.ioNamePtr = (StringPtr)volName;
  243.             pb.ioVRefNum = vRefNum;
  244.             error = PBDTOpenInform(&pb);
  245.             /* PBDTOpenInform informs us if the desktop was just created */
  246.             /* by leaving the low bit of ioTagInfo clear (0) */
  247.             *newDTDatabase = ((pb.ioTagInfo & 1L) == 0);
  248.             if ( error == paramErr )
  249.             {
  250.                 error = PBDTGetPath(&pb);
  251.                 /* PBDTGetPath doesn't tell us if the database is new */
  252.                 /* so assume it is not new */
  253.                 *newDTDatabase = false;
  254.             }
  255.             *dtRefNum = pb.ioDTRefNum;
  256.         }
  257.         else
  258.         {
  259.             error = paramErr;
  260.         }
  261.     }
  262.     return ( error );
  263. }
  264.  
  265. /*****************************************************************************/
  266.  
  267. /*
  268. **    GetAPPLFromDesktopFile
  269. **
  270. **    Get a application's location from the
  271. **    Desktop file's 'APPL' resources.
  272. */
  273. static    OSErr    GetAPPLFromDesktopFile(ConstStr255Param volName,
  274.                                        short vRefNum,
  275.                                        OSType creator,
  276.                                        short *applVRefNum,
  277.                                        long *applParID,
  278.                                        Str255 applName)
  279. {
  280.     OSErr error;
  281.     short realVRefNum;
  282.     Str255 desktopName;
  283.     short savedResFile;
  284.     short dfRefNum;
  285.     Handle applResHandle;
  286.     Boolean foundCreator;
  287.     Ptr applPtr;
  288.     long applSize;
  289.     
  290.     error = DetermineVRefNum(volName, vRefNum, &realVRefNum);
  291.     if ( error == noErr )
  292.     {
  293.         error = GetDesktopFileName(realVRefNum, desktopName);
  294.         if ( error == noErr )
  295.         {
  296.             savedResFile = CurResFile();
  297.             /*
  298.             **    Open the 'Desktop' file in the root directory. (because
  299.             **    opening the resource file could preload unwanted resources,
  300.             **    bracket the call with SetResLoad(s))
  301.             */
  302.             SetResLoad(false);
  303.             dfRefNum = HOpenResFile(realVRefNum, fsRtDirID, desktopName, fsRdPerm);
  304.             SetResLoad(true);
  305.             
  306.             if ( dfRefNum != -1)
  307.             {
  308.                 /* Get 'APPL' resource ID 0 */
  309.                 applResHandle = Get1Resource(kAPPLResType, 0);
  310.                 if ( applResHandle != NULL )
  311.                 {
  312.                     applSize = GetHandleSize((Handle)applResHandle);
  313.                     if ( applSize != 0 )    /* make sure the APPL resource isn't empty */
  314.                     {
  315.                         foundCreator = false;
  316.                         applPtr = *applResHandle;
  317.                         
  318.                         /* APPL's don't have a count so I have to use the size as the bounds */
  319.                         while ( (foundCreator == false) &&
  320.                                 (applPtr < (*applResHandle + applSize)) )
  321.                         {
  322.                             if ( ((APPLRecPtr)applPtr)->creator == creator )
  323.                             {
  324.                                 foundCreator = true;
  325.                             }
  326.                             else
  327.                             {
  328.                                 /* fun with pointer math... */
  329.                                 applPtr += sizeof(OSType) +
  330.                                            sizeof(long) +
  331.                                            ((APPLRecPtr)applPtr)->applName[0] + 1;
  332.                                 /* application mappings are word aligned within the resource */
  333.                                 if ( ((unsigned long)applPtr % 2) != 0 )
  334.                                 {
  335.                                     applPtr += 1;
  336.                                 }
  337.                             }
  338.                         }
  339.                         if ( foundCreator == true )
  340.                         {
  341.                             *applVRefNum = realVRefNum;
  342.                             *applParID = ((APPLRecPtr)applPtr)->parID;
  343.                             BlockMoveData(((APPLRecPtr)applPtr)->applName,
  344.                                           applName,
  345.                                           ((APPLRecPtr)applPtr)->applName[0] + 1);
  346.                             /* error is already noErr */
  347.                         }
  348.                         else
  349.                         {
  350.                             error = afpItemNotFound;    /* didn't find a creator match */
  351.                         }
  352.                     }
  353.                     else
  354.                     {
  355.                         error = afpItemNotFound;    /* no APPL mapping available */
  356.                     }
  357.                 }
  358.                 else
  359.                 {
  360.                     error = afpItemNotFound;    /* no APPL mapping available */
  361.                 }
  362.                 
  363.                 /* restore the resource chain and close the Desktop file */
  364.                 UseResFile(savedResFile);
  365.                 CloseResFile(dfRefNum);
  366.             }
  367.             else
  368.             {
  369.                 error = afpItemNotFound;
  370.             }
  371.         }
  372.     }
  373.     
  374.     return ( error );
  375. }
  376.  
  377. /*****************************************************************************/
  378.  
  379. pascal    OSErr    DTXGetAPPL(ConstStr255Param volName,
  380.                            short vRefNum,
  381.                            OSType creator,
  382.                            Boolean searchCatalog,
  383.                            short *applVRefNum,
  384.                            long *applParID,
  385.                            Str255 applName)
  386. {
  387.     OSErr error;
  388.     UniversalFMPB pb;
  389.     short dtRefNum;
  390.     Boolean newDTDatabase;
  391.     short realVRefNum;
  392.     short index;
  393.     Boolean applFound;
  394.     FSSpec spec;
  395.     long actMatchCount;
  396.     
  397.     /* get the real vRefNum */
  398.     error = DetermineVRefNum(volName, vRefNum, &realVRefNum);
  399.     if ( error == noErr )
  400.     {
  401.         error = DTOpen(volName, vRefNum, &dtRefNum, &newDTDatabase);
  402.         if ( error == noErr )
  403.         {
  404.             if ( !newDTDatabase )
  405.             {
  406.                 index = 0;
  407.                 applFound = false;
  408.                 do
  409.                 {
  410.                     pb.dtPB.ioNamePtr = applName;
  411.                     pb.dtPB.ioDTRefNum = dtRefNum;
  412.                     pb.dtPB.ioIndex = index;
  413.                     pb.dtPB.ioFileCreator = creator;
  414.                     error = PBDTGetAPPLSync(&pb.dtPB);
  415.                     if ( error == noErr )
  416.                     {
  417.                         /* got a match - see if it is valid */
  418.                         
  419.                         *applVRefNum = realVRefNum; /* get the vRefNum now */
  420.                         *applParID = pb.dtPB.ioAPPLParID; /* get the parent ID now */
  421.     
  422.                         /* pb.hPB.fileParam.ioNamePtr is already set */
  423.                         pb.hPB.fileParam.ioVRefNum = realVRefNum;
  424.                         pb.hPB.fileParam.ioFVersNum = 0;
  425.                         pb.hPB.fileParam.ioDirID = *applParID;
  426.                         pb.hPB.fileParam.ioFDirIndex = 0;    /* use ioNamePtr and ioDirID */
  427.                         if ( PBHGetFInfoSync(&pb.hPB) == noErr )
  428.                         {
  429.                             if ( (pb.hPB.fileParam.ioFlFndrInfo.fdCreator == creator) &&
  430.                                  (pb.hPB.fileParam.ioFlFndrInfo.fdType == 'APPL') )
  431.                             {
  432.                                 applFound = true;
  433.                             }
  434.                         }
  435.                     }
  436.                     ++index;
  437.                 } while ( (error == noErr) && !applFound );
  438.                 if ( error != noErr )
  439.                 {
  440.                     error = afpItemNotFound;
  441.                 }
  442.             }
  443.             else
  444.             {
  445.                 /* Desktop database is empty (new), set error to try CatSearch */
  446.                 error = afpItemNotFound;
  447.             }
  448.         }
  449.         /* acceptable errors from Desktop Manager to continue are paramErr or afpItemNotFound */
  450.         if ( error == paramErr )
  451.         {
  452.             /* if paramErr, the volume didn't support the Desktop Manager */
  453.             /* try the Desktop file */
  454.             
  455.             error = GetAPPLFromDesktopFile(volName, vRefNum, creator,
  456.                                             applVRefNum, applParID, applName);
  457.             if ( error == noErr )
  458.             {
  459.                 /* got a match - see if it is valid */
  460.                 
  461.                 pb.hPB.fileParam.ioNamePtr = applName;
  462.                 pb.hPB.fileParam.ioVRefNum = *applVRefNum;
  463.                 pb.hPB.fileParam.ioFVersNum = 0;
  464.                 pb.hPB.fileParam.ioDirID = *applParID;
  465.                 pb.hPB.fileParam.ioFDirIndex = 0;    /* use ioNamePtr and ioDirID */
  466.                 if ( PBHGetFInfoSync(&pb.hPB) == noErr )
  467.                 {
  468.                     if ( (pb.hPB.fileParam.ioFlFndrInfo.fdCreator != creator) ||
  469.                          (pb.hPB.fileParam.ioFlFndrInfo.fdType != 'APPL') )
  470.                     {
  471.                         error = afpItemNotFound;
  472.                     }
  473.                 }
  474.                 else if ( error == fnfErr )
  475.                 {
  476.                     error = afpItemNotFound;
  477.                 }
  478.             }
  479.         }
  480.         /* acceptable error from DesktopFile code to continue is afpItemNotFound */
  481.         if ( (error == afpItemNotFound) && searchCatalog)
  482.         {
  483.             /* Couldn't be found in the Desktop file either, */
  484.             /* try searching with CatSearch if requested */
  485.             
  486.             error = CreatorTypeFileSearch(NULL, realVRefNum, creator, kAPPLResType, &spec, 1,
  487.                                             &actMatchCount, true);
  488.             if ( (error == noErr) || (error == eofErr) )
  489.             {
  490.                 if ( actMatchCount > 0 )
  491.                 {
  492.                     *applVRefNum = spec.vRefNum;
  493.                     *applParID = spec.parID;
  494.                     BlockMoveData(spec.name, applName, spec.name[0] + 1);
  495.                 }
  496.                 else
  497.                 {
  498.                     error = afpItemNotFound;
  499.                 }
  500.             }
  501.         }
  502.     }
  503.     
  504.     return ( error );
  505. }
  506.  
  507. /*****************************************************************************/
  508.  
  509. pascal    OSErr    DTGetAPPL(ConstStr255Param volName,
  510.                           short vRefNum,
  511.                           OSType creator,
  512.                           short *applVRefNum,
  513.                           long *applParID,
  514.                           Str255 applName)
  515. {
  516.     /* Call DTXGetAPPL with the "searchCatalog" parameter true */ 
  517.     return ( DTXGetAPPL(volName, vRefNum, creator, true,
  518.                         applVRefNum, applParID, applName) );
  519. }
  520.  
  521. /*****************************************************************************/
  522.  
  523. pascal    OSErr    FSpDTGetAPPL(ConstStr255Param volName,
  524.                              short vRefNum,
  525.                              OSType creator,
  526.                              FSSpec *spec)
  527. {
  528.     return ( DTGetAPPL(volName, vRefNum, creator,
  529.                         &(spec->vRefNum), &(spec->parID), spec->name) );
  530. }
  531.  
  532. /*****************************************************************************/
  533.  
  534. /*
  535. **    FindBundleGivenCreator
  536. **
  537. **    Search the current resource file for the 'BNDL' resource with the given
  538. **    creator and return a handle to it.
  539. */
  540. static    OSErr    FindBundleGivenCreator(OSType creator,
  541.                                        BNDLRecHandle *returnBndl)
  542. {
  543.     OSErr            error;
  544.     short            numOfBundles;
  545.     short            index;
  546.     BNDLRecHandle    theBndl;
  547.     
  548.     error = afpItemNotFound;    /* default to not found */
  549.     
  550.     /* Search each BNDL resource until we find the one with a matching creator. */
  551.     
  552.     numOfBundles = Count1Resources(kBNDLResType);
  553.     index = 1;
  554.     *returnBndl = NULL;
  555.     
  556.     while ( (index <= numOfBundles) && (*returnBndl == NULL) )
  557.     {
  558.         theBndl = (BNDLRecHandle)Get1IndResource(kBNDLResType, index);
  559.         
  560.         if ( theBndl != NULL )
  561.         {
  562.             if ( (*theBndl)->signature == creator )
  563.             {
  564.                 /* numTypes and typeArray->count will always be the actual count minus 1, */
  565.                 /* so 0 in both fields is valid. */
  566.                 if ( ((*theBndl)->numTypes >= 0) && ((*theBndl)->typeArray->count >= 0) )
  567.                 {
  568.                     /* got it */
  569.                     *returnBndl = theBndl;
  570.                     error = noErr;
  571.                 }
  572.             }
  573.         }    
  574.         
  575.         index ++;
  576.     }
  577.     
  578.     return ( error );
  579. }
  580.  
  581. /*****************************************************************************/
  582.  
  583. /*
  584. **    FindTypeInBundle
  585. **
  586. **    Given a Handle to a BNDL return a pointer to the desired type
  587. **    in it. If the type is not found, or if the type's count < 0,
  588. **    return afpItemNotFound.
  589. */
  590. static    OSErr    FindTypeInBundle(OSType typeToFind,
  591.                                  BNDLRecHandle theBndl,
  592.                                  BundleTypePtr *returnBundleType)
  593. {
  594.     OSErr            error;
  595.     short            index;
  596.     Ptr                ptrIterator;    /* use a Ptr so we can do ugly pointer math */
  597.     
  598.     error = afpItemNotFound;    /* default to not found */
  599.     
  600.     ptrIterator = (Ptr)((*theBndl)->typeArray);
  601.     index = 0;
  602.     *returnBundleType = NULL;
  603.  
  604.     while ( (index < ((*theBndl)->numTypes + 1)) &&
  605.             (*returnBundleType == NULL) )
  606.     {
  607.         if ( (((BundleTypePtr)ptrIterator)->type == typeToFind) &&
  608.              (((BundleTypePtr)ptrIterator)->count >= 0) )
  609.         {
  610.                 *returnBundleType = (BundleTypePtr)ptrIterator;
  611.                 error = noErr;
  612.         }
  613.         else
  614.         {
  615.             ptrIterator += ( sizeof(OSType) +
  616.                              sizeof(short) +
  617.                              ( sizeof(IDRec) * (((BundleTypePtr)ptrIterator)->count + 1) ) );
  618.             ++index;
  619.         }
  620.     }
  621.         
  622.     return ( error );
  623. }
  624.  
  625. /*****************************************************************************/
  626.  
  627. /*
  628. **    GetLocalIDFromFREF
  629. **
  630. **    Given a pointer to a 'FREF' BundleType record, load each 'FREF' resource
  631. **    looking for a matching fileType. If a matching fileType is found, return
  632. **    its icon local ID. If no match is found, return afpItemNotFound as the
  633. **    function result.
  634. */
  635. static    OSErr    GetLocalIDFromFREF(BundleTypePtr theBundleType,
  636.                                    OSType fileType,
  637.                                    short *iconLocalID)
  638. {
  639.     OSErr            error;
  640.     short            index;
  641.     IDRecPtr        idIterator;
  642.     FREFRecHandle    theFref;
  643.     
  644.     error = afpItemNotFound;    /* default to not found */
  645.     
  646.     /* For each localID in this type, get the FREF resource looking for fileType */
  647.     index = 0;
  648.     idIterator = &theBundleType->idArray[0];
  649.     *iconLocalID = 0;
  650.     
  651.     while ( (index <= theBundleType->count) && (*iconLocalID == 0) )
  652.     {
  653.         theFref = (FREFRecHandle)Get1Resource(kFREFResType, idIterator->rsrcID);
  654.         if ( theFref != NULL )
  655.         {
  656.             if ( (*theFref)->fileType == fileType )
  657.             {
  658.                 *iconLocalID = (*theFref)->iconID;
  659.                 error = noErr;
  660.             }
  661.         }
  662.         
  663.         ++idIterator;
  664.         ++index;
  665.     }
  666.     
  667.     return ( error );
  668. }
  669.  
  670. /*****************************************************************************/
  671.  
  672. /*
  673. **    GetIconRsrcIDFromLocalID
  674. **
  675. **    Given a pointer to a 'ICN#' BundleType record, look for the IDRec with
  676. **    the localID that matches iconLocalID. If a matching IDRec is found,
  677. **    return the IDRec's rsrcID field value. If no match is found, return
  678. **    afpItemNotFound as the function result.
  679. */
  680. static    OSErr    GetIconRsrcIDFromLocalID(BundleTypePtr theBundleType,
  681.                                          short iconLocalID,
  682.                                          short *iconRsrcID)
  683. {
  684.     OSErr        error;
  685.     short        index;
  686.     IDRecPtr    idIterator;
  687.     
  688.     error = afpItemNotFound;    /* default to not found */
  689.     
  690.     /* Find the rsrcID of the icon family type, given the localID */
  691.     index = 0;
  692.     idIterator = &theBundleType->idArray[0];
  693.     *iconRsrcID = 0;
  694.     
  695.     while ( (index <= theBundleType->count) && (*iconRsrcID == 0) )
  696.     {
  697.         if ( idIterator->localID == iconLocalID )
  698.         {
  699.             *iconRsrcID = idIterator->rsrcID;
  700.             error = noErr;
  701.         }
  702.         
  703.         idIterator ++;
  704.         index ++;
  705.     }
  706.     
  707.     return ( error );
  708. }
  709.  
  710. /*****************************************************************************/
  711.  
  712. /*
  713. **    DTIconToResIcon
  714. **
  715. **    Map a Desktop Manager icon type to the corresponding resource type.
  716. **    Return (OSType)0 if there is no corresponding resource type.
  717. */
  718. static    OSType    DTIconToResIcon(short iconType)
  719. {
  720.     OSType    resType;
  721.     
  722.     switch ( iconType )
  723.     {
  724.         case kLargeIcon:
  725.             resType = large1BitMask;
  726.             break;
  727.         case kLarge4BitIcon:
  728.             resType = large4BitData;
  729.             break;
  730.         case kLarge8BitIcon:
  731.             resType = large8BitData;
  732.             break;
  733.         case kSmallIcon:
  734.             resType = small1BitMask;
  735.             break;
  736.         case kSmall4BitIcon:
  737.             resType = small4BitData;
  738.             break;
  739.         case kSmall8BitIcon:
  740.             resType = small8BitData;
  741.             break;
  742.         default:
  743.             resType = (OSType)0;
  744.             break;
  745.     }
  746.     
  747.     return ( resType );
  748. }
  749.  
  750. /*****************************************************************************/
  751.  
  752. /*
  753. **    GetIconFromDesktopFile
  754. **
  755. **    INPUT a pointer to a non-existent Handle, because we'll allocate one
  756. **
  757. **    search each BNDL resource for the right fileCreator and once we get it
  758. **        find the 'FREF' type in BNDL
  759. **        for each localID in the type, open the FREF resource
  760. **            if the FREF is the desired fileType
  761. **                get its icon localID
  762. **                get the ICN# type in BNDL
  763. **                get the icon resource number from the icon localID
  764. **                get the icon resource type from the desktop mgr's iconType
  765. **                get the icon of that type and number
  766. */
  767. static    OSErr    GetIconFromDesktopFile(ConstStr255Param volName,
  768.                                        short vRefNum,
  769.                                        short iconType,
  770.                                        OSType fileCreator,
  771.                                        OSType fileType,
  772.                                        Handle *iconHandle)
  773. {
  774.     OSErr            error;
  775.     short            realVRefNum;
  776.     Str255            desktopName;
  777.     short            savedResFile;
  778.     short            dfRefNum;
  779.     BNDLRecHandle    theBndl = NULL;
  780.     BundleTypePtr    theBundleType;
  781.     short            iconLocalID;
  782.     short            iconRsrcID;
  783.     OSType            iconRsrcType;
  784.     Handle            returnIconHandle;    
  785.     char            bndlState;
  786.     
  787.     *iconHandle = NULL;
  788.     
  789.     error = DetermineVRefNum(volName, vRefNum, &realVRefNum);
  790.     if ( error == noErr )
  791.     {
  792.         error = GetDesktopFileName(realVRefNum, desktopName);
  793.         if ( error == noErr )
  794.         {
  795.             savedResFile = CurResFile();
  796.         
  797.             /*
  798.             **    Open the 'Desktop' file in the root directory. (because
  799.             **    opening the resource file could preload unwanted resources,
  800.             **    bracket the call with SetResLoad(s))
  801.             */
  802.             SetResLoad(false);
  803.             dfRefNum = HOpenResFile(realVRefNum, fsRtDirID, desktopName, fsRdPerm);
  804.             SetResLoad(true);
  805.         
  806.             if ( dfRefNum != -1 )
  807.             {
  808.                 /*
  809.                 **    Find the BNDL resource with the specified creator.
  810.                 */
  811.                 error = FindBundleGivenCreator(fileCreator, &theBndl);
  812.                 if ( error == noErr )
  813.                 {
  814.                     /* Lock the BNDL resource so it won't be purged when other resources are loaded */
  815.                     bndlState = HGetState((Handle)theBndl);
  816.                     HLock((Handle)theBndl);
  817.                     
  818.                     /* Find the 'FREF' BundleType record in the BNDL resource. */
  819.                     error = FindTypeInBundle(kFREFResType, theBndl, &theBundleType);
  820.                     if ( error == noErr )
  821.                     {
  822.                         /* Find the local ID in the 'FREF' resource with the specified fileType */
  823.                         error = GetLocalIDFromFREF(theBundleType, fileType, &iconLocalID);
  824.                         if ( error == noErr )
  825.                         {
  826.                             /* Find the 'ICN#' BundleType record in the BNDL resource. */
  827.                             error = FindTypeInBundle(kIconFamResType, theBndl, &theBundleType);
  828.                             if ( error == noErr )
  829.                             {
  830.                                 /* Find the icon's resource ID in the 'ICN#' BundleType record */
  831.                                 error = GetIconRsrcIDFromLocalID(theBundleType, iconLocalID, &iconRsrcID);
  832.                                 if ( error == noErr )
  833.                                 {
  834.                                     /* Map Desktop Manager icon type to resource type */
  835.                                     iconRsrcType = DTIconToResIcon(iconType);
  836.                                     
  837.                                     if ( iconRsrcType != (OSType)0 )
  838.                                     {
  839.                                         /* Load the icon */
  840.                                         returnIconHandle = Get1Resource(iconRsrcType, iconRsrcID);
  841.                                         if ( returnIconHandle != NULL )
  842.                                         {
  843.                                             /* Copy the resource handle, and return the copy */
  844.                                             HandToHand(&returnIconHandle);
  845.                                             if ( MemError() == noErr )
  846.                                             {
  847.                                                 *iconHandle = returnIconHandle;
  848.                                             }
  849.                                             else
  850.                                             {
  851.                                                 error = afpItemNotFound;
  852.                                             }
  853.                                         }
  854.                                         else
  855.                                         {
  856.                                             error = afpItemNotFound;
  857.                                         }
  858.                                     }
  859.                                 }
  860.                             }
  861.                         }
  862.                     }
  863.                     /* Restore the state of the BNDL resource */ 
  864.                     HSetState((Handle)theBndl, bndlState);
  865.                 }
  866.                 /* Restore the resource chain and close the Desktop file */
  867.                 UseResFile(savedResFile);
  868.                 CloseResFile(dfRefNum);
  869.             }
  870.             else
  871.             {
  872.                 error = ResError(); /* could not open Desktop file */
  873.             }
  874.         }
  875.         if ( (error != noErr) && (error != memFullErr) )
  876.         {
  877.             error = afpItemNotFound;    /* force an error we should return */
  878.         }
  879.     }
  880.     
  881.     return ( error );
  882. }
  883.  
  884. /*****************************************************************************/
  885.  
  886. pascal    OSErr    DTGetIcon(ConstStr255Param volName,
  887.                           short vRefNum,
  888.                           short iconType,
  889.                           OSType fileCreator,
  890.                           OSType fileType,
  891.                           Handle *iconHandle)
  892. {
  893.     OSErr error;
  894.     DTPBRec pb;
  895.     short dtRefNum;
  896.     Boolean newDTDatabase;
  897.     Size bufferSize;
  898.     
  899.     *iconHandle = NULL;
  900.     error = DTOpen(volName, vRefNum, &dtRefNum, &newDTDatabase);
  901.     if ( error == noErr )
  902.     {
  903.         /* there was a desktop database and it's now open */
  904.         
  905.         if ( !newDTDatabase )    /* don't bother to look in a new (empty) database */
  906.         {
  907.             /* get the buffer size for the requested icon type */
  908.             switch ( iconType )
  909.             {
  910.                 case kLargeIcon:
  911.                     bufferSize = kLargeIconSize;
  912.                     break;
  913.                 case kLarge4BitIcon:
  914.                     bufferSize = kLarge4BitIconSize;
  915.                     break;
  916.                 case kLarge8BitIcon:
  917.                     bufferSize = kLarge8BitIconSize;
  918.                     break;
  919.                 case kSmallIcon:
  920.                     bufferSize = kSmallIconSize;
  921.                     break;
  922.                 case kSmall4BitIcon:
  923.                     bufferSize = kSmall4BitIconSize;
  924.                     break;
  925.                 case kSmall8BitIcon:
  926.                     bufferSize = kSmall8BitIconSize;
  927.                     break;
  928.                 default:
  929.                     iconType = 0;
  930.                     bufferSize = 0;
  931.                     break;
  932.             }
  933.             if ( bufferSize != 0 )
  934.             {
  935.                 *iconHandle = NewHandle(bufferSize);
  936.                 if ( *iconHandle != NULL )
  937.                 {
  938.                     HLock(*iconHandle);
  939.         
  940.                     pb.ioDTRefNum = dtRefNum;
  941.                     pb.ioTagInfo = 0;
  942.                     pb.ioDTBuffer = **iconHandle;
  943.                     pb.ioDTReqCount = bufferSize;
  944.                     pb.ioIconType = iconType;
  945.                     pb.ioFileCreator = fileCreator;
  946.                     pb.ioFileType = fileType;
  947.                     error = PBDTGetIconSync(&pb);
  948.     
  949.                     HUnlock(*iconHandle);
  950.                     
  951.                     if ( error != noErr )
  952.                     {
  953.                         DisposeHandle(*iconHandle);    /* dispose of the allocated memory */
  954.                         *iconHandle = NULL;
  955.                     }
  956.                 }
  957.                 else
  958.                 {
  959.                     error = memFullErr;    /* handle could not be allocated */
  960.                 }
  961.             }
  962.             else
  963.             {
  964.                 error = paramErr;    /* unknown icon type requested */
  965.             }
  966.         }
  967.         else
  968.         {
  969.             error = afpItemNotFound;    /* the desktop database was empty - nothing to return */
  970.         }
  971.     }
  972.     else
  973.     {
  974.         /* There is no desktop database - try the Desktop file */
  975.         
  976.         error = GetIconFromDesktopFile(volName, vRefNum, iconType,
  977.                                         fileCreator, fileType, iconHandle);
  978.     }
  979.     
  980.     return ( error );
  981. }
  982.  
  983. /*****************************************************************************/
  984.  
  985. pascal    OSErr    DTSetComment(short vRefNum,
  986.                              long dirID,
  987.                              ConstStr255Param name,
  988.                              ConstStr255Param comment)
  989. {
  990.     DTPBRec pb;
  991.     OSErr error;
  992.     short dtRefNum;
  993.     Boolean newDTDatabase;
  994.  
  995.     error = DTOpen(name, vRefNum, &dtRefNum, &newDTDatabase);
  996.     if ( error == noErr )
  997.     {
  998.         pb.ioDTRefNum = dtRefNum;
  999.         pb.ioNamePtr = (StringPtr)name;
  1000.         pb.ioDirID = dirID;
  1001.         pb.ioDTBuffer = (Ptr)&comment[1];
  1002.         /* Truncate the comment to 200 characters just in case */
  1003.         /* some file system doesn't range check */
  1004.         if ( comment[0] <= 200 )
  1005.         {
  1006.             pb.ioDTReqCount = comment[0];
  1007.         }
  1008.         else
  1009.         {
  1010.             pb.ioDTReqCount = 200;
  1011.         }
  1012.         error = PBDTSetCommentSync(&pb);
  1013.     }
  1014.     return (error);
  1015. }
  1016.  
  1017. /*****************************************************************************/
  1018.  
  1019. pascal    OSErr    FSpDTSetComment(const FSSpec *spec,
  1020.                               ConstStr255Param comment)
  1021. {
  1022.     return (DTSetComment(spec->vRefNum, spec->parID, spec->name, comment));
  1023. }
  1024.  
  1025. /*****************************************************************************/
  1026.  
  1027. /*
  1028. **    GetCommentID
  1029. **
  1030. **    Get the comment ID number for the Desktop file's 'FCMT' resource ID from
  1031. **    the file or folders fdComment (frComment) field.
  1032. */
  1033. static    OSErr    GetCommentID(short vRefNum,
  1034.                              long dirID,
  1035.                              ConstStr255Param name,
  1036.                              short *commentID)
  1037. {
  1038.     CInfoPBRec pb;
  1039.     OSErr error;
  1040.  
  1041.     error = GetCatInfoNoName(vRefNum, dirID, name, &pb);
  1042.     *commentID = pb.hFileInfo.ioFlXFndrInfo.fdComment;
  1043.     return ( error );
  1044. }
  1045.  
  1046. /*****************************************************************************/
  1047.  
  1048. /*
  1049. **    GetCommentFromDesktopFile
  1050. **
  1051. **    Get a file or directory's Finder comment field (if any) from the
  1052. **    Desktop file's 'FCMT' resources.
  1053. */
  1054. static    OSErr    GetCommentFromDesktopFile(short vRefNum,
  1055.                                           long dirID,
  1056.                                           ConstStr255Param name,
  1057.                                           Str255 comment)
  1058. {
  1059.     OSErr error;
  1060.     short commentID;
  1061.     short realVRefNum;
  1062.     Str255 desktopName;
  1063.     short savedResFile;
  1064.     short dfRefNum;
  1065.     StringHandle commentHandle;
  1066.     
  1067.     /* Get the comment ID number */
  1068.     error = GetCommentID(vRefNum, dirID, name, &commentID);
  1069.     if ( error == noErr )
  1070.     {
  1071.         if ( commentID != 0 )    /* commentID == 0 means there's no comment */
  1072.         {
  1073.             error = DetermineVRefNum(name, vRefNum, &realVRefNum);
  1074.             if ( error == noErr )
  1075.             {
  1076.                 error = GetDesktopFileName(realVRefNum, desktopName);
  1077.                 if ( error == noErr )
  1078.                 {
  1079.                     savedResFile = CurResFile();
  1080.                     /*
  1081.                     **    Open the 'Desktop' file in the root directory. (because
  1082.                     **    opening the resource file could preload unwanted resources,
  1083.                     **    bracket the call with SetResLoad(s))
  1084.                     */
  1085.                     SetResLoad(false);
  1086.                     dfRefNum = HOpenResFile(realVRefNum, fsRtDirID, desktopName, fsRdPerm);
  1087.                     SetResLoad(true);
  1088.                     
  1089.                     if ( dfRefNum != -1)
  1090.                     {
  1091.                         /* Get the comment resource */
  1092.                         commentHandle = (StringHandle)Get1Resource(kFCMTResType,commentID);
  1093.                         if ( commentHandle != NULL )
  1094.                         {
  1095.                             if ( GetHandleSize((Handle)commentHandle) > 0 )
  1096.                             {
  1097.                                 BlockMoveData(*commentHandle, comment, *commentHandle[0] + 1);
  1098.                             }
  1099.                             else
  1100.                             {
  1101.                                 error = afpItemNotFound;    /* no comment available */
  1102.                             }
  1103.                         }
  1104.                         else
  1105.                         {
  1106.                             error = afpItemNotFound;    /* no comment available */
  1107.                         }
  1108.                         
  1109.                         /* restore the resource chain and close the Desktop file */
  1110.                         UseResFile(savedResFile);
  1111.                         CloseResFile(dfRefNum);
  1112.                     }
  1113.                     else
  1114.                     {
  1115.                         error = afpItemNotFound;
  1116.                     }
  1117.                 }
  1118.                 else
  1119.                 {
  1120.                     error = afpItemNotFound;
  1121.                 }
  1122.             }
  1123.         }
  1124.         else
  1125.         {
  1126.             error = afpItemNotFound;    /* no comment available */
  1127.         }
  1128.     }
  1129.     
  1130.     return ( error );
  1131. }
  1132.  
  1133. /*****************************************************************************/
  1134.  
  1135. pascal    OSErr    DTGetComment(short vRefNum,
  1136.                              long dirID,
  1137.                              ConstStr255Param name,
  1138.                              Str255 comment)
  1139. {
  1140.     DTPBRec pb;
  1141.     OSErr error;
  1142.     short dtRefNum;
  1143.     Boolean newDTDatabase;
  1144.  
  1145.     if (comment != NULL)
  1146.     {
  1147.         comment[0] = 0;    /* return nothing by default */
  1148.         
  1149.         /* attempt to open the desktop database */
  1150.         error = DTOpen(name, vRefNum, &dtRefNum, &newDTDatabase);
  1151.         if ( error == noErr )
  1152.         {
  1153.             /* There was a desktop database and it's now open */
  1154.             
  1155.             if ( !newDTDatabase )
  1156.             {
  1157.                 pb.ioDTRefNum = dtRefNum;
  1158.                 pb.ioNamePtr = (StringPtr)name;
  1159.                 pb.ioDirID = dirID;
  1160.                 pb.ioDTBuffer = (Ptr)&comment[1];
  1161.                 /*
  1162.                 **    IMPORTANT NOTE #1: Inside Macintosh says that comments
  1163.                 **    are up to 200 characters. While that may be correct for
  1164.                 **    the HFS file system's Desktop Manager, other file
  1165.                 **    systems (such as Apple Photo Access) return up to
  1166.                 **    255 characters. Make sure the comment buffer is a Str255
  1167.                 **    or you'll regret it.
  1168.                 **
  1169.                 **    IMPORTANT NOTE #2: Although Inside Macintosh doesn't
  1170.                 **    mention it, ioDTReqCount is a input field to
  1171.                 **    PBDTGetCommentSync. Some file systems (like HFS) ignore
  1172.                 **    ioDTReqCount and always return the full comment --
  1173.                 **    others (like AppleShare) respect ioDTReqCount and only
  1174.                 **    return up to ioDTReqCount characters of the comment.
  1175.                 */
  1176.                 pb.ioDTReqCount = sizeof(Str255) - 1;
  1177.                 error = PBDTGetCommentSync(&pb);
  1178.                 if (error == noErr)
  1179.                 {
  1180.                     comment[0] = (unsigned char)pb.ioDTActCount;
  1181.                 }
  1182.             }
  1183.         }
  1184.         else
  1185.         {
  1186.             /* There is no desktop database - try the Desktop file */
  1187.             error = GetCommentFromDesktopFile(vRefNum, dirID, name, comment);
  1188.             if ( error != noErr )
  1189.             {
  1190.                 error = afpItemNotFound;    /* return an expected error */
  1191.             }
  1192.         }
  1193.     }
  1194.     else
  1195.     {
  1196.         error = paramErr;
  1197.     }
  1198.     
  1199.     return (error);
  1200. }
  1201.  
  1202. /*****************************************************************************/
  1203.  
  1204. pascal    OSErr    FSpDTGetComment(const FSSpec *spec,
  1205.                               Str255 comment)
  1206. {
  1207.     return (DTGetComment(spec->vRefNum, spec->parID, spec->name, comment));
  1208. }
  1209.  
  1210. /*****************************************************************************/
  1211.  
  1212. pascal    OSErr    DTCopyComment(short srcVRefNum,
  1213.                               long srcDirID,
  1214.                               ConstStr255Param srcName,
  1215.                               short dstVRefNum,
  1216.                               long dstDirID,
  1217.                               ConstStr255Param dstName)
  1218. /* The destination volume must support the Desktop Manager for this to work */
  1219. {
  1220.     OSErr error;
  1221.     Str255 comment;
  1222.  
  1223.     error = DTGetComment(srcVRefNum, srcDirID, srcName, comment);
  1224.     if ( (error == noErr) && (comment[0] > 0) )
  1225.     {
  1226.         error = DTSetComment(dstVRefNum, dstDirID, dstName, comment);
  1227.     }
  1228.     return (error);
  1229. }
  1230.  
  1231. /*****************************************************************************/
  1232.  
  1233. pascal    OSErr    FSpDTCopyComment(const FSSpec *srcSpec,
  1234.                                const FSSpec *dstSpec)
  1235. /* The destination volume must support the Desktop Manager for this to work */
  1236. {
  1237.     return (DTCopyComment(srcSpec->vRefNum, srcSpec->parID, srcSpec->name,
  1238.                         dstSpec->vRefNum, dstSpec->parID, dstSpec->name));
  1239. }
  1240.  
  1241. /*****************************************************************************/
  1242.